﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using PetaPoco;

namespace DAL.MySQL
{
    public class adminDAL
    {
        private Database db = DbScoure.DataBase();
        public adminEntity GetOne(int id)
        {
           return db.SingleOrDefault<adminEntity>("select * from admin where id=@0", id);
        }
        public adminEntity GetOneByUN(string username,string password)
        {
            return db.SingleOrDefault<adminEntity>("select * from admin where username=@0 and password=@1 and endtime>" + Com.MysqlSqlite.GetDateTime() + " and utype>0", username,password);
        }

        public List<adminEntity> GetList(string where,string order,params object[] args)
        {
            var list = db.Fetch<adminEntity>("select * from admin where 1=1 " + where+" order by "+order, args);
            return list;
        }

        public Page<adminEntity> GetPageList(int pagenum,int pagesize,string where,string order, params object[] args)
        {
            var pagelist=db.Page<adminEntity>(pagenum,pagesize,"select * from admin where 1=1 "+where + " order by " + order, args);
            return pagelist;
        }

        public int Update(object obj, int ID)
        {
            return db.Update("admin", "ID", obj, ID);
        }
        public adminEntity Save(adminEntity model)
        {
            db.Save("admin", "ID", model);
            return model;
        }

        public bool Del(int id)
        {
            try
            {
                db.Delete("admin", "ID", null, id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool Del(adminEntity model)
        {
            try
            {
                db.Delete("admin", "ID", model);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
